<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业物联网转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82882996 15899929162
// +----------------------------------------------------------------------
namespace app\common\dao\property\bill;

use app\common\dao\BaseDao;
use app\common\model\property\bill\PropertyBillDayModel;

class PropertyBillDayDao extends BaseDao
{

    /**
     * @return PropertyBillDayModel
     */
    protected function getModel(): string
    {
        return PropertyBillDayModel::class;
    }

    public function search(array $where, int $propertyId = null)
    {
        return $this->getModel()::getDB()
            ->when($propertyId !== null, function ($query) use ($propertyId) {
                $query->where('property_id', $propertyId);
            })
            ->when(isset($where['day_time']) && $where['day_time'] !== '', function ($query) use ($where) {
                $query->where('day_time', $where['day_time']);
            })
            ->when(!empty($where['month']) || !empty($where['year']), function ($query) use ($where) {
                if (!empty($where['year'])) {
                    $query->whereBetween('month_time', [$where['year'] . '-1-1', $where['year'] . '-12-' . date('t', strtotime($where['year'] . '-12-1'))]);
                } elseif (!empty($where['month'])) {
                    $query->whereBetween('month_time', [$where['month'] . '-1', $where['month'] . '-' . date('t', strtotime($where['month'] . '-1'))]);
                }
            })
            ->when(isset($where['keywords']) && $where['keywords'] !== '', function ($query) use ($where) {
                $query->whereLike('remark', '%' . trim($where['keywords']) . '%');
            });
    }

    public function deleteByPropertyId(int $id)
    {
        return $this->getSearch([
            'property_id' => $id
        ])->delete();
    }
}
